<?php
class Khachhang_model extends CI_Model{
    private $tbl='khachhang';
            function __construct() {
        parent::__construct();
    }
    function insert_KH($data1,$data2,$table2_name) {
        $DB=$this->load->database('khachhang',TRUE);
      
        $sql=$DB->insert_string($this->tbl,$data1);
        $DB->query($sql);
        
        $khachhang=$DB->get_where($this->tbl,array('RAND_CODE'=>$DB->escape($data1['RAND_CODE'])))->row_array();
        
        $DB->where('MSKH',$DB->escape($khachhang['MSKH']));
        $DB->update($this->tbl,array('RAND_CODE'=>"''")); 
        unset($khachhang['RAND_CODE']);
        
        $data2['MSKH']=$khachhang['MSKH'];
        
        $sql=$DB->insert_string($table2_name,$data2);
        $DB->query($sql);
        
        $khachhang2=$DB->get_where($table2_name,array('MSKH'=>$DB->escape($khachhang['MSKH'])))->row_array();
        return array_merge($khachhang,$khachhang2);
    }
    function load_by_MSCB($MSCB) {
        $DB=$this->load->database('nhanvien',TRUE);
        $MSCB=$DB->escape($MSCB);
        $DB->from($this->tbl);
        $DB->where($this->tbl.'.MSCB',$MSCB);
        $DB->join('KHACHHANGNL',$this->tbl.'.MSKH = KHACHHANGNL.MSKH');
        $DB->join('PHIVCHH',$this->tbl.'.MSPHH = PHIVCHH.MSPHH');
        $resultNL=$DB->get()->result_array();
        
        $DB->from($this->tbl);
        $DB->where($this->tbl.'.MSCB',$MSCB);
        $DB->join('KHACHHANGTE',$this->tbl.'.MSKH = KHACHHANGTE.MSKH');
        $DB->join('PHIVCHH',$this->tbl.'.MSPHH = PHIVCHH.MSPHH');
        $resultTE=$DB->get()->result_array();
        
        return array_merge($resultNL,$resultTE);
    }
    function load_by_id($MSKH,$is_join=TRUE) {
        
        $DB=$this->load->database('nhanvien',TRUE);
        $MSKH=$DB->escape($MSKH);
        $DB->from($this->tbl);
        $DB->where($this->tbl.'.MSKH',$MSKH);
        if ($is_join)
        {
            if (substr($MSKH, 1,2)=="NL")
            {
                $DB->join('KHACHHANGNL',$this->tbl.'.MSKH = KHACHHANGNL.MSKH');
            }else{
                $DB->join('KHACHHANGTE',$this->tbl.'.MSKH = KHACHHANGTE.MSKH');
            }
        }
        $DB->join('GHEKHACH',  $this->tbl.'.MSKH=GHEKHACH.MSKH');
        $DB->join('PHIVCHH',$this->tbl.'.MSPHH = PHIVCHH.MSPHH');
        $DB->join('TRANGTHAITG',$this->tbl.'.MSTTTG = TRANGTHAITG.MSTTTG');
        return $DB->get();
    }
    function update_KLHH($MSKH,$KLvuot) {
        $DB=$this->load->database('nhanvien',TRUE);
        $DB->where('MSKH',$DB->escape($MSKH));
        $DB->update($this->tbl,array('KHOILUONGVUOT'=>$KLvuot));
    }
    function check($MSKH,$PASS){
        $DB=$this->load->database('khachhang',TRUE);
        $result=$DB->get_where($this->tbl,array('MSKH'=>$DB->escape($MSKH),'PASSWORD'=>$DB->escape($PASS)))->row_array();
        if (count($result)>0){
            return TRUE;
        }
        return FALSE;
    }
}
